﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqModel;
using FoneBuddy.LinqData;

public partial class Add_City : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    Country country = new Country();
    State state = new State();

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getCountry();

            if (Request.QueryString["Id"] != null)
            {
                lblHeader.Text = "Edit City";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add City";
            }
        }
    }
    #endregion

    #region save clear and cancel
    protected void Save(object sender, EventArgs e)
    {
        City city = new City();
        try
        {
         
            city = new City(_dataObject);
            BuildObject(city);
            city.GetCityByName();

            if (Request.QueryString["Id"] != null)
            {
                if (city.Id != Convert.ToInt32(Request.QueryString["Id"]) && city.Id != 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> City is already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(city);
                city.Id = Convert.ToInt32(Request.QueryString["Id"]);
                city.IsDeleted = false;
                city.Update();

                divMessage.InnerHtml = "<strong>Success!</strong> City is Updated successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
            else
            {
                if (city.Id > 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> City is already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                BuildObject(city);
                city.CreatedDate = System.DateTime.Now;
                city.Create();

                divMessage.InnerHtml = "<strong>Success!</strong> City is added successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
                ClearControls();
            }
         

        }
        catch (Exception ex)
        {
        }
    }
    private void ClearControls()
    {
        drpCountry.ClearSelection();
        drpState.Items.Clear();
        drpState.Items.Insert(0, new ListItem("Select", "0"));
        txtCityName.Text = string.Empty;
        drpCountry.Enabled = true;
        drpState.Enabled = true;

    }

    #endregion

    #region getdata
    private void getCountry()
    {
        List<Country> countries = new List<Country>();
        country = new Country(_dataObject);

        countries = country.GetAllCountry();

        if (countries != null)
        {
            drpCountry.DataSource = countries;
            drpCountry.DataBind();
            drpCountry.Items.Insert(0, new ListItem("Select", "0"));
            drpState.Items.Insert(0, new ListItem("Select", "0"));
        }
    }
    private void getState()
    {
        State state = new State();
        state = new State(_dataObject);
        List<State> states = new List<State>();
        state.CountryId = Convert.ToInt32(drpCountry.SelectedValue);
        states = state.GetStateByCountryId();

        if (states != null)
        {
            drpState.DataSource = states;
            drpState.DataBind();
            if (states.Count == 0)
                drpState.Items.Insert(0, new ListItem("Select", "0"));
        }

    }
    private void BindData()
    {
        City city = new City();
        city = new City(_dataObject);
        // List<State> states = new List<State>();
        city.Id = Convert.ToInt32(Request.QueryString["Id"]);
        city.GetCityById();
        txtCityName.Text = city.Name;
        drpCountry.SelectedValue = city.CountryId.ToString();
        getState();
        drpState.SelectedValue = city.StateId.ToString();
        drpCountry.Enabled = false;
        drpState.Enabled = false;
    }
    private void BuildObject(City city)
    {
        city.Name = txtCityName.Text;
        city.CountryId = Convert.ToInt32(drpCountry.SelectedValue);
        city.StateId = Convert.ToInt32(drpState.SelectedValue);
    }

    private void BuildCityObject(City city)
    {
        city.Name = txtCityName.Text;
        city.CountryId = Convert.ToInt32(drpCountry.SelectedValue);
        city.StateId = Convert.ToInt32(drpState.SelectedValue);
        city.GetCityByName();
    }
    #endregion

    #region changeevent
    protected void OnCountryChange(object sender, EventArgs e)
    {
        getState();
    }
    #endregion

}